home *** CD-ROM | disk | FTP | other *** search
/ Delphi Magazine Collection 2001 / Delphi Magazine Collection 20001 (2001).iso / DISKS / ISSUE08 / DATADICT / OLDUTILS.PAS < prev    next >
Encoding:
Pascal/Delphi Source File  |  1995-10-09  |  11.1 KB  |  301 lines

  1. unit Oldutils;
  2. interface
  3. const
  4.   DevScreenWidth: LongInt = 1024; {design modes}
  5.   DevScreenHeight: LongInt = 768;
  6.   DevPixelsPerInch : Longint = 120;
  7. type                            { from DOS.PAS, RTL 5.5, BP7}
  8.   PathStr = string[79];         { File pathname string }
  9.   DirStr  = string[67];         { Drive and directory string }
  10.   NameStr = string[8];          { File name string }
  11.   ExtStr  = string[4];          { File extension string }
  12. var
  13.   pathholder : pathstr;
  14.   Dirholder  : dirstr;
  15.   nameholder : namestr;
  16.   extholder  : extstr;
  17.  
  18.  
  19.  
  20. function StringAsPChar(var S: OpenString): PChar;
  21. function AddBackSlash(const S: String): String;
  22. function StripBackSlash(const S: String): String;
  23. function WinExecAndWait(Path: String; Visibility: word): word;
  24. function GetEnvVar(EnvVar: String): String;
  25. procedure CopyFile(Source, Dest: String);
  26. function appName : string;                         {my addition}
  27. function noSlashstring(const s: string): string;
  28. procedure FSplit(Path: PathStr; var Dir: DirStr;   {from DOS.PAS}
  29.   var Name: NameStr; var Ext: ExtStr);
  30. function ThisFileDateTime(fname : string): tdatetime;
  31.  
  32. Procedure ScaleForm(sender: Tobject);   {from TI 2861, scaling forms}
  33. implementation
  34.  
  35. uses SysUtils, LZExpand, WinTypes, WinProcs, Forms, Controls,
  36.      stdctrls, buttons, graphics, dbctrls;
  37.  
  38.  
  39. function StringAsPChar(var S: OpenString): PChar;
  40. { This function null-terminates a string so that it can be passed to functions }
  41. { that require PChar types. If string is longer than 254 chars, then it will   }
  42. { be truncated to 254. }
  43. begin
  44.   if Length(S) = High(S) then Dec(S[0]); { Truncate S if it's too long }
  45.   S[Ord(Length(S)) + 1] := #0;           { Place null at end of string }
  46.   Result := @S[1];                       { Return "PChar'd" string }
  47. end;
  48.  
  49.  
  50. function AddBackSlash(const S: String): String;
  51. { Adds a backslash to string S.  If S is already 255 chars or already has }
  52. { trailing backslash, then function returns S. }
  53. begin
  54.   if (Length(S) < 255) and (S[Length(S)] <> '\') then
  55.     Result := S + '\'
  56.   else
  57.     Result := S;
  58. end;
  59.  
  60. function StripBackSlash(const S: String): String;
  61. { Removes trailing backslash from S, if one exists }
  62. begin
  63.   Result := S;
  64.   if Result[Length(Result)] = '\' then
  65.     Dec(Result[0]);
  66. end;
  67.  
  68. function WinExecAndWait(Path: String; Visibility: word): word;
  69. var
  70.   InstanceID : THandle;
  71. begin
  72.   { Convert String to PChar, and try to run the application }
  73.   InstanceID := WinExec(StringAsPChar(Path),Visibility);
  74.   if InstanceID < 32 then { a value less than 32 indicates an Exec error }
  75.      WinExecAndWait := InstanceID
  76.   else begin
  77.     repeat
  78.       Application.ProcessMessages;
  79.     until Application.Terminated or (GetModuleUsage(InstanceID) = 0);
  80.     WinExecAndWait := 32;
  81.   end;
  82. end;
  83.  
  84. function GetEnvVar(EnvVar: String): String;
  85. { Returns the value of the DOS environment variable passed in EnvVar.       }
  86. { Note: EnvVar must be 253 chars or less, or it will be truncated to 253.   }
  87. { Note2: Under Win32, the GetEnvironmentVariable() function should be used. }
  88. var
  89.   P: PChar;
  90. begin
  91.   Result := '';                                { return empty string on fail }
  92.   P := GetDOSEnvironment;                      { retrieve pointer to env vars }
  93.   if EnvVar[0] > #253 then EnvVar[0] := #253;  { truncate if too long }
  94.   EnvVar := EnvVar + '=';                      { append "=" sign to string }
  95.   StringAsPChar(EnvVar);                       { add null-terminator }
  96.   while P^ <> #0 do
  97.     { does first environment variable match EnvVar? }
  98.     if StrLIComp(P, @EnvVar[1], Length(EnvVar)) <> 0 then
  99.       inc(P, StrLen(P) + 1)                    { if not, then go to next }
  100.     else begin
  101.       inc(P, Length(EnvVar));                  { if so, the get value }
  102.       Result := StrPas(P);                     { return a string }
  103.       Break;                                   { get out of loop }
  104.     end;
  105. end;
  106.  
  107. procedure CopyFile(Source, Dest: String);
  108. var
  109.   SourceHand, DestHand: Integer;
  110.   OpenBuf: TOFStruct;
  111. begin
  112.   { Open source file, and pass our psuedo-PChar as the filename }
  113.   SourceHand := LZOpenFile(StringAsPChar(Source), OpenBuf, of_Share_Deny_Write or of_Read);
  114.   { raise an exception on error }
  115.   if SourceHand = -1 then
  116.     raise EInOutError.Create('Error opening source file "' + Source + '"');
  117.   try
  118.     { Open destination file, and pass our psuedo-PChar as the filename }
  119.     DestHand := LZOpenFile(StringAsPChar(Dest), OpenBuf, of_Share_Exclusive or of_Write
  120.                            or of_Create);
  121.     { Check for error and raise exception }
  122.     if DestHand = -1 then
  123.       raise EInOutError.CreateFmt('Error opening destination file "%s"',[Dest]);
  124.     try
  125.       { copy source to dest, raise exception on error }
  126.       if LZCopy(SourceHand, DestHand) < 0 then
  127.         raise EInOutError.CreateFmt('Error copying file "%s"', [Source]);
  128.     finally
  129.       { whether or not an exception occurs, we need to close the files }
  130.       LZClose(DestHand);
  131.     end;
  132.   finally
  133.     LZClose(SourceHand);
  134.   end;
  135. end;
  136.  
  137. function appName : string;
  138. begin
  139.   result := copy(application.exename, 1, pos('.',application.exename)-1);
  140. end;
  141.  
  142. function noSlashstring(const s: string): string;
  143. {assumes s is a fully qualified filename}
  144. {takes out '\' and '.'}
  145. {alias name max is dbmaxnamelen,31}
  146. var extra : integer;
  147. begin
  148.   result := s[1]+copy(s,3,255);   {extract the :}
  149.   while pos('\',result) <> 0 do
  150.     result := copy(result, 1, pos('\',result)-1)+
  151.               copy(result, pos('\', result)+1, 255);
  152.   result := copy(result, 1, pos('.', result)-1) +
  153.             copy(result, pos('.', result)+1, 255);   
  154.   extra := length(result) - 31;
  155.   if extra > 0
  156.     then result := result[1] + copy(result, extra+1, 255);
  157. end;
  158.  
  159.  
  160.  
  161. {$L SPLT.OBJ}           { File name split routine }
  162.  {brought in without changes from my old BP7 files}
  163.  {I suppose Delphi must have something similar, but so far it's
  164.   been hiding from me...}
  165. procedure FSplit(Path: PathStr; var Dir: DirStr;
  166.   var Name: NameStr; var Ext: ExtStr); external {SPLT};
  167.  
  168. function ThisFileDateTime(fname : string): tdatetime;
  169.   var searchRec : TsearchRec;
  170. begin
  171.    FindFirst(fname, faAnyfile, SearchRec);
  172.    result := fileDateToDateTime(SearchRec.time);
  173. end;
  174.  
  175. function CurrentTextWidth(cur_canvas : tcanvas; const whatstr : string): integer;
  176.  var TextMetric : tTextMetric;
  177. begin
  178.   getTextMetrics(cur_canvas.handle, textMetric);
  179.   result := (textMetric.tmAveCharWidth * length(whatstr))+2;
  180. end;
  181.  
  182. function CurrentTextHeight(cur_canvas : tcanvas): integer;
  183.  var TextMetric : tTextMetric;
  184. begin
  185.   getTextMetrics(cur_canvas.handle, textMetric);
  186.   result := textMetric.tmHeight + textMetric.tmExternalLeading;
  187. end;
  188.  
  189.  
  190. Procedure ScaleForm(sender: Tobject);
  191.   {this is stuff from one of Borland's TI docs.  Doesn't do a very good job...}
  192. var
  193.   i: integer;  {used by font scaler}
  194.   cur_canvas : tcanvas;
  195.   cur_items, cur_width, org_width : integer;
  196. begin
  197.  with sender as tform do begin
  198.    scaled := true;
  199. {   AutoScroll := false;  {true = 'don't change the form's frame size at runtime' }
  200.    position := poScreenCenter;
  201.    if true {screen.width = DevScreenWidth}
  202.      then begin
  203.        if (width > (screen.width-3)) or (height > (screen.height -3))
  204.            then windowState := wsMaximized;
  205. {           else scaleby(DevPixelsPerInch, Screen.PixelsPerInch);}
  206.        for i := componentCount - 1 downto 0 do
  207.          begin
  208.            if components[i] is Tlabel
  209.              then  with components[i] as Tlabel do
  210.                begin
  211.                  font.height := height - 7;
  212.                  {
  213.                  cur_canvas := canvas;
  214.                  if font.height > height
  215.                    then font.height := font.height - (height - font.height-1);
  216.                  width := currentTextWidth(cur_canvas, caption);
  217.                  Height := currentTextHeight(cur_canvas)+2;
  218.                  {adjust width pitch}
  219.                  {textMetrics }
  220.                  end;
  221.            if components[i] is TEdit
  222.              then  with components[i] as TEdit do
  223.                begin
  224.                  font.height := height - 7; 
  225.                  {cur_canvas := canvas;}
  226.                  {height := canvas.textHeight('M')+3;}
  227.                 { width := currentTextWidth(cur_canvas, caption);}
  228.                  {Height := currentTextHeight(cur_canvas)+2;}
  229.                  {if abs(font.height) > height
  230.                    then font.height := abs(font.height) - (height - abs(font.height)-2);}
  231.                  end;
  232.            if components[i] is TListBox
  233.              then  with components[i] as TListBox do
  234.                begin
  235.                  if font.height > Itemheight
  236.                    then font.height := font.height - (Itemheight - font.height-1);
  237.                  end;
  238.            if components[i] is TdbRadioGroup
  239.              then  with components[i] as TdbRadioGroup do
  240.                begin
  241.                  cur_canvas := canvas;
  242.                  org_width := 0;
  243.                  for cur_items := 0 to items.count -1 do
  244.                    begin
  245.                      cur_width := currentTextWidth(cur_canvas, items[cur_items]+values[cur_items]);
  246.                      cur_width := cur_width + 32;  {width of checkbox?}
  247.                      if cur_width > org_width
  248.                        then org_width := cur_width;
  249.                      end;
  250.                  width := org_width;
  251.                  Height := (currentTextHeight(cur_canvas)+1)*items.count-1;
  252.                  end;
  253.            if components[i] is Tbutton
  254.              then  with components[i] as Tbutton do
  255.                 begin
  256.                   {examin width of actual text to width of box
  257.                    and ajdust; same for height}
  258.                 end;
  259.            end;
  260.        end;
  261.    end;
  262. end;
  263.  
  264. end.
  265. { from help on components    for I := 0 to ComponentCount -1 do
  266.      if Components[I] is TButton then
  267.        TButton(Components[I]).Font.Name := 'Courier';
  268.   Edit1.Text := IntToStr(ComponentCount) + ' components';
  269. end;}
  270.  
  271. {  from typinfo.int...
  272. function GetTypeData(TypeInfo: PTypeInfo): PTypeData;
  273.  
  274. function GetEnumName(TypeInfo: PTypeInfo; Value: Integer): PString;
  275. function GetEnumValue(TypeInfo: PTypeInfo; const EnumName: string): Integer;
  276.  
  277. function GetPropInfo(TypeInfo: PTypeInfo; const PropName: string): PPropInfo;
  278. procedure GetPropInfos(TypeInfo: PTypeInfo; PropList: PPropList);
  279. function GetPropList(TypeInfo: PTypeInfo; TypeKinds: TTypeKinds;
  280.   PropList: PPropList): Integer;
  281.  
  282. function IsStoredProp(Instance: TObject; PropInfo: PPropInfo): Boolean;
  283.  
  284. function GetOrdProp(Instance: TObject; PropInfo: PPropInfo): Longint;
  285. procedure SetOrdProp(Instance: TObject; PropInfo: PPropInfo;
  286.   Value: Longint);
  287.  
  288. function GetStrProp(Instance: TObject; PropInfo: PPropInfo): string;
  289. procedure SetStrProp(Instance: TObject; PropInfo: PPropInfo;
  290.   const Value: string);
  291.  
  292. function GetFloatProp(Instance: TObject; PropInfo: PPropInfo): Extended;
  293. procedure SetFloatProp(Instance: TObject; PropInfo: PPropInfo;
  294.   Value: Extended);
  295.  
  296. function GetMethodProp(Instance: TObject; PropInfo: PPropInfo): TMethod;
  297. procedure SetMethodProp(Instance: TObject; PropInfo: PPropInfo;
  298.   const Value: TMethod);
  299.  
  300. }
  301.